﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Generic;
using MapCenterSystem.DataInfo;
using MapCenterSytem.Process;

using System.Threading;
using System.Resources;
using System.Globalization;

namespace MobiMap.Admin
{
    public partial class serviceTypeUC : System.Web.UI.UserControl
    {
        ResourceManager rm;
        CultureInfo ci;

        string add = string.Empty;
        string edit = string.Empty;
        string delete = string.Empty;
        string update = string.Empty;
        string cancel = string.Empty;
        string placetypeName = string.Empty;
        string placetypePicture = string.Empty;
        string messageAddSuccess = string.Empty;
        string messageAddError = string.Empty;

        private IList<PlaceTypeInfo> lstPlaceTypeInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Language
            SetLanguage();

            lbMessage.Text = "";
            if (!IsPostBack)
            {
                Cache.Remove("mobimapAdmin_listServiceType");
                BindListServiceType();                
            }            
        }

        public void SetLanguage()
        {
            rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
            if (Request.Cookies["language"] != null)
            {
                string language = Request.Cookies["language"].Value;
                Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
                LoadString(Thread.CurrentThread.CurrentCulture);

                return;
            }

            //Defaut language is Vietnamese
            Thread.CurrentThread.CurrentCulture = new CultureInfo("vi-VN");
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
        }

        public void LoadString(CultureInfo ci)
        {
            lbServiceType_lstType.Text = rm.GetString("Admin_ServiceType_lbList", ci);
            lbServiceType_Add_Title.Text = rm.GetString("Admin_FunctionAdd_Title", ci);
            btnAddNew.Text = rm.GetString("Admin_Add", ci);
            edit = rm.GetString("Admin_Edit", ci);
            update = rm.GetString("Admin_Update", ci);
            delete = rm.GetString("Admin_Delete", ci);
            cancel = rm.GetString("Admin_Cancel", ci);
            messageAddError = rm.GetString("Admin_AddError", ci);
            messageAddSuccess = rm.GetString("Admin_AddSuccess", ci);
            placetypeName = rm.GetString("Admin_ServiceType_Name", ci);
            placetypePicture = rm.GetString("Admin_ServiceType_Picture", ci);
            lbPlaceTypeName.InnerHtml = placetypeName;
            lbPlaceTypePicture.InnerHtml = placetypePicture;

            hddConfirm.Value = rm.GetString("Admin_Confirm", ci);
            
        }

        public void BindListServiceType()
        {
            lstPlaceTypeInfo = new List<PlaceTypeInfo>();
            if (Cache["mobimapAdmin_listServiceType"] == null)
            {
                lstPlaceTypeInfo = PlaceTypeEx.GetListPlaceType();
                Cache["mobimapAdmin_listServiceType"] = lstPlaceTypeInfo;
            }
            else
            {
                lstPlaceTypeInfo = (IList<PlaceTypeInfo>)Cache["mobimapAdmin_listServiceType"];
            }

            gvServiceType.DataSource = lstPlaceTypeInfo;
            gvServiceType.DataBind();

            gvServiceType.HeaderRow.Cells[1].Text = placetypeName;
            gvServiceType.HeaderRow.Cells[2].Text = placetypePicture;
        }

        protected void gvServiceType_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvServiceType.EditIndex = -1;
            gvServiceType.PageIndex = e.NewPageIndex;
            BindListServiceType();            
        }

        private int _iRow = 0;
        protected void gvServiceType_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                
                Image imgPic = (Image)e.Row.FindControl("imgPic");
                Label lbPic = (Label)e.Row.FindControl("lbPic");

                string baseDir = "../Icons/PlaceType/";
                imgPic.ImageUrl = baseDir + lbPic.Text;
                //imgPic.ToolTip = imgPic.ImageUrl;
                if (_iRow == gvServiceType.EditIndex)
                    imgPic.Visible = false;

                if (_iRow != gvServiceType.EditIndex)
                {
                    ((LinkButton)e.Row.FindControl("lbtEdit")).Text = edit;
                    ((LinkButton)e.Row.FindControl("lbtDelete")).Text = delete;
                }
                else
                {
                    ((LinkButton)e.Row.FindControl("lbtUpdate")).Text = update;
                    ((LinkButton)e.Row.FindControl("lbtCancel")).Text = cancel;
                }

                _iRow++;
            }
        }
                
        protected void gvServiceType_RowEditing(object sender, GridViewEditEventArgs e)
        {            
            gvServiceType.EditIndex = e.NewEditIndex;            
            BindListServiceType();            
        }

        protected void gvServiceType_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvServiceType.EditIndex = -1;
            BindListServiceType();
        }

        protected void gvServiceType_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtDes = (TextBox)gvServiceType.Rows[e.RowIndex].FindControl("txtDes");
            FileUpload filePic = (FileUpload)gvServiceType.Rows[e.RowIndex].FindControl("filePicture");
            Label lbID = (Label)gvServiceType.Rows[e.RowIndex].FindControl("lbID");
            Label lbPic = (Label)gvServiceType.Rows[e.RowIndex].FindControl("lbPic");

            int placeID = int.Parse(lbID.Text);
            bool result = PlaceTypeEx.UpdatePlaceType(new PlaceTypeInfo(placeID, txtDes.Text));
            if (result == true)
            {
                if(filePic.PostedFile.FileName != "")
                {
                    string picName = "placetype_" + placeID + ".png";
                    string filename = AppDomain.CurrentDomain.BaseDirectory 
                                + @"Icons\PlaceType\" + picName;
                    filePic.PostedFile.SaveAs(filename);

                    if(lbPic.Text != picName)
                        PlaceTypeEx.UpdatePlaceTypePicture(new PlaceTypeInfo(placeID, "", picName));
                }
            }

            Cache.Remove("mobimapAdmin_listServiceType");
            gvServiceType.EditIndex = -1;
            BindListServiceType();
        }

        protected void gvServiceType_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label lbID = (Label)gvServiceType.Rows[e.RowIndex].FindControl("lbID");
            bool result = PlaceTypeEx.DeletePlaceType(int.Parse(lbID.Text));
            if (result)
            {
                Session["mobimapAdmin_tabselected"] = "ServiceType";
                Cache.Remove("mobimapAdmin_listServiceType");
                BindListServiceType();
                lbMessage.Text = messageAddSuccess;
            }
            else
                lbMessage.Text = messageAddError;
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            string des = txtNewDes.Text;
            string picPath = filePic.Value;
            
            int result = PlaceTypeEx.AddPlaceType(new PlaceTypeInfo(des));
            if (result != -1)
            {
                if (filePic.PostedFile.FileName != "")
                {
                    string picName = "placetype_" + result + ".png";
                    string filename = AppDomain.CurrentDomain.BaseDirectory
                                + @"Icons\PlaceType\" + picName;
                    filePic.PostedFile.SaveAs(filename);
                    PlaceTypeEx.UpdatePlaceTypePicture(new PlaceTypeInfo(result, "", picName));
                }

                Cache.Remove("mobimapAdmin_listServiceType");
                BindListServiceType();
                lbMessage.Text = messageAddSuccess;
            }
            else
                lbMessage.Text = messageAddError;
        }

        protected void gvServiceType_Init(object sender, EventArgs e)
        {
            //CommandField commandfield = (CommandField)gvServiceType.Columns[gvServiceType.Columns.Count - 1];
            //commandfield.EditText = "Sửa";
            //commandfield.DeleteText = "Xóa";
            //commandfield.UpdateText = "Lưu";
            //commandfield.CancelText = "Thoát";
        }

        
    }
}